<report title="D2D 5.0 Diabetes Care - Statin" description="Reports diabetic patients on a Statin" active="1">

<query> 

SELECT DISTINCT 
        d.demographic_no as Demographic,
        CONCAT(d.last_name, ', ', d.first_name) AS Patient, 
        d.sex AS Sex, 
        floor(datediff(now(),concat(d.year_of_birth,'-',d.month_of_birth,'-',d.date_of_birth))/365) AS Age, 
        concat(p.last_name, ', ', p.first_name) AS MRP, 
        a1c.dataField AS "A1C Value", 
        fbs.dataField AS "FBS Value"

FROM demographic d

# provider name
INNER JOIN 
    (SELECT provider_no, last_name, first_name FROM provider 
        WHERE provider_no LIKE '{provider}' 
        AND status='1' 
        AND provider_type='doctor' 
        AND ohip_no>1) as p
ON d.provider_no = p.provider_no

# count of billings for K030A and Q040A
LEFT JOIN (
    SELECT d.demographic_no, bi.service_code as code, COUNT(*) as c
    FROM billing_on_item bi, billing_on_cheader1 bch, demographic d
    WHERE bi.service_code  IN ('K030A', 'Q040A')
    AND bch.demographic_no = d.demographic_no
    AND bch.id = bi.ch1_id
    GROUP BY d.demographic_no, bi.service_code
) as billing
ON d.demographic_no = billing.demographic_no

# any diagnoses
LEFT JOIN (
    SELECT demographic_no, dxresearch_code, status FROM dxresearch
) as dx
ON d.demographic_no = dx.demographic_no


# list of non-archived drugs
LEFT JOIN
(SELECT demographic_no, BN FROM drugs WHERE archived <> 1) dr
ON d.demographic_no = dr.demographic_no

# most recent A1C value
LEFT JOIN 
(SELECT m.demographicNo, m.type, m.dataField, m.dateObserved
FROM measurements m
INNER JOIN (SELECT demographicNo, type, MAX(dateObserved) AS dateObserved
FROM measurements
WHERE type = "A1C"
AND dateObserved >= DATE_SUB(now(),INTERVAL 12 Month)
GROUP BY demographicNo, type) y
ON m.demographicNo = y.demographicNo
AND m.type = y.type
AND m.dateObserved = y.dateObserved) a1c
ON d.demographic_no = a1c.demographicNo

# most recent FBS value
LEFT JOIN 
(SELECT m.demographicNo, m.type, m.dataField, m.dateObserved
FROM measurements m
INNER JOIN (SELECT demographicNo, type, MAX(dateObserved) AS dateObserved
FROM measurements
WHERE type = "FBS"
AND dateObserved >= DATE_SUB(now(),INTERVAL 12 Month)
GROUP BY demographicNo, type) y
ON m.demographicNo = y.demographicNo
AND m.type = y.type
AND m.dateObserved = y.dateObserved) fbs
ON d.demographic_no = fbs.demographicNo

/* Criteria:
 - Active patient
 - Does not have prediabetes or gestational diabetes
 - Has any of the following criteria:
    - non-archived prescription for a diabetes medication
    - A1C >= 7%
    - FBS >= 7%
    - diabetes diagnosis
    - billed Q040A > 0 times or K030A > 1 time
*/
WHERE d.patient_status = "AC"
AND d.demographic_no NOT IN (
    SELECT DISTINCT demographic_no from dxresearch 
    WHERE dxresearch_code IN ('2564', '6480', '6488', '249', '7751', '7902') AND status = "A"
)
AND (dr.BN 
    like '%statin%' 
    or dr.BN like '%lipitor%' 
    or dr.BN like '%caduet%' 
    or dr.BN like '%norvasc%' 
    or dr.BN like '%amlodipine%' 
    or dr.BN like '%lescol%' 
    or dr.BN like '%crestor%' 
    or dr.BN like '%zocor%' 
    or dr.BN like '%pravachol%' 
    or dr.BN like '%torvast%'
)
AND (
    (   dr.BN LIKE '%Acarbose%' 
        OR dr.BN LIKE '%Actos%' 
        OR dr.BN LIKE '%Amaryl%' 
        OR dr.BN LIKE '%Avandia%' 
        OR dr.BN LIKE '%byetta%' 
        OR dr.BN LIKE '%Detamir%' 
        OR dr.BN LIKE '%diamicron%' 
        OR dr.BN LIKE '%Glibenclamide%'
        OR dr.BN LIKE '%Glimepiride%'
        OR dr.BN LIKE '%glucobay%' 
        OR dr.BN LIKE '%gluconorm%'
        OR dr.BN LIKE '%glucophage%'
        OR dr.BN LIKE '%Glyburide%' 
        OR dr.BN LIKE '%Humalog%'
        OR dr.BN LIKE '%Humulin%' 
        OR dr.BN LIKE '%Insulin%' 
        OR dr.BN LIKE '%Januvia%'
        OR dr.BN LIKE '%Liraglutide%' 
        OR dr.BN LIKE '%Linagliptin%' 
        OR dr.BN LIKE '%Metformin%'
        OR dr.BN LIKE '%Nateglinide%' 
        OR dr.BN LIKE '%Onglyza%' 
        OR dr.BN LIKE '%Pioglitazone%' 
        OR dr.BN LIKE '%Prandase%'
        OR dr.BN LIKE '%Repaglinide%' 
        OR dr.BN LIKE '%Saxaglyptin%' 
        OR dr.BN LIKE '%Sitagliptin%' 
        OR dr.BN LIKE '%starlix%' 
        OR dr.BN LIKE '%Tolbutamide%' 
        OR dr.BN LIKE '%Victoza%'
    )
    OR (a1c.datafield >= 7 or (a1c.datafield >= 0.07 and a1c.datafield < 1))
    OR (fbs.datafield >= 7 or (fbs.datafield >= 0.07 and fbs.datafield < 1))
    OR (dx.dxresearch_code IN ('250', '2500', 'E10', 'E11', 'DB-610') AND dx.status = "A")
    OR (billing.code = 'Q040A' OR (billing.code = 'K030A' and billing.c > 1))
)
ORDER by d.last_name


</query>

<param id="provider" type="list" description="Provider"> 
    <param-query>
    select "%", "All Physicians"
    UNION
    (select provider_no, last_name from provider WHERE status='1' 
        AND provider_type='doctor' 
        AND ohip_no>1
    ORDER BY last_name);</param-query> 
</param>

</report>